/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package onpu_dms_xls.kernel.common.pdf.composite;

import com.itextpdf.text.pdf.PdfContentByte;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Arrays;
import onpu_dms_xls.kernel.common.Util;
import onpu_dms_xls.kernel.common.logAndDebug.LoggingFacade;
import org.apache.log4j.Logger;

/**
 *
 * @author snbm
 */
public class ExtractTextLabel extends ExtractLabel {
    

    private int virtColLeftBorder;
    private int virtColRightBorder;
    private double oneYColumnHigh;

    public ExtractTextLabel(String n) {
        super(n);
        LoggingFacade.trace.debug("PDF_TextLabel constructor n=" + n);
    }

    @Override
    public void printToPDFWriter(PDF_ExtractWriter pdf_ew) {
        //no virtual column alignment
        if (getVirtColLeftBorder() == 0 && getVirtColRightBorder() == 0) {
            pdf_ew.addTask(new PDF_ExtractWriterTask(getPageNum(), getFont(), getCodePage(), getFontSize(), pdf_ew.getVariableValueForLabel(this), getX(), getY()));
        } //virtual column alignment
        else {
            //Debug.print("\tmakeFile FUCK printing:"+label.getVariableName());
            final double ONE_CHAR_SIZE = Util.get_ONE_CHAR_SIZE(this);

            ArrayList<String> words = Util.getWordsFromString(pdf_ew.getVariableValueForLabel(this));

            LoggingFacade.trace.debug("first" + Arrays.toString(words.toArray(new String[]{})));
            int firstLineLehgthInChars = (int) ((getVirtColRightBorder() - getX()) / ONE_CHAR_SIZE);
            int wordIndex = 0;
            StringBuilder firstLine = new StringBuilder();
            while (true) {
                if (wordIndex == words.size()) {
                    pdf_ew.addTask(new PDF_ExtractWriterTask(getPageNum(), getFont(), getCodePage(), getFontSize(), firstLine.toString(), getX(), getY()));
                    break;
                }
                if ((firstLine.length() + words.get(wordIndex).length()) > firstLineLehgthInChars) {
                    pdf_ew.addTask(new PDF_ExtractWriterTask(getPageNum(), getFont(), getCodePage(), getFontSize(), firstLine.toString(), getX(), getY()));
                    break;
                }
                firstLine.append(words.get(wordIndex));
                wordIndex++;
            }
            if (wordIndex == words.size()) {
                return;
            }

            LoggingFacade.trace.debug("next" + Arrays.toString(words.subList(wordIndex, words.size()-1).toArray(new String[]{})));
            //wordIndex here - the index of word to be inserted (NOT INSERTED yet)
            //go to next lines
            int nextLinesLengthInChars = (int) ((getVirtColRightBorder() - getVirtColLeftBorder()) / ONE_CHAR_SIZE);
            int Y_linesAdjuster = 1;

            while (true) {
                StringBuilder nextLine = new StringBuilder();
                while (true) {
                    if (wordIndex == words.size()) {
                        pdf_ew.addTask(new PDF_ExtractWriterTask(getPageNum(), getFont(), getCodePage(), getFontSize(), nextLine.toString(), getVirtColLeftBorder(), (int) (getY() - Y_linesAdjuster * oneYColumnHigh)));
                        break;
                    }
                    if ((nextLine.length() + words.get(wordIndex).length()) > nextLinesLengthInChars) {
                        pdf_ew.addTask(new PDF_ExtractWriterTask(getPageNum(), getFont(), getCodePage(), getFontSize(), nextLine.toString(), getVirtColLeftBorder(), (int) (getY() - Y_linesAdjuster * oneYColumnHigh)));
                        break;
                    }
                    nextLine.append(words.get(wordIndex));
                    wordIndex++;
                }
                if (wordIndex == words.size()) {
                    break;
                }
                Y_linesAdjuster++;
            }
        }
        return;
    }

    @Override
    public void print() {
        LoggingFacade.trace.debug("PDF_TextLabel print, varName=" + getVariableName());
    }

    @Override
    public Iterator<ExtractComponent> getIterator() {
        return (new ArrayList<ExtractComponent>()).iterator();
    }

    /**
     * @return the virtColLeftBorder
     */
    public int getVirtColLeftBorder() {
        return virtColLeftBorder;
    }

    /**
     * @param virtColLeftBorder the virtColLeftBorder to set
     */
    public void setVirtColLeftBorder(int virtColLeftBorder) {
        this.virtColLeftBorder = virtColLeftBorder;
    }

    /**
     * @return the virtColRightBorder
     */
    public int getVirtColRightBorder() {
        return virtColRightBorder;
    }

    /**
     * @param virtColRightBorder the virtColRightBorder to set
     */
    public void setVirtColRightBorder(int virtColRightBorder) {
        this.virtColRightBorder = virtColRightBorder;
    }

    /**
     * @return the oneYColumnHigh
     */
    public double getOneYColumnHigh() {
        return oneYColumnHigh;
    }

    /**
     * @param oneYColumnHigh the oneYColumnHigh to set
     */
    public void setOneYColumnHigh(double oneYColumnHigh) {
        this.oneYColumnHigh = oneYColumnHigh;
    }
}

